from emmobileapi import sys_strategy_fund as sys_fund
from LSTM import data_source as ds
from LSTM import predict_stock as ps
from emmongo import em_mongomanager as mongoManager
from LSTM.variableset import EMVar

def run():
    sys_fund.sys_all_strategy_fund()
    result = mongoManager.db[mongoManager.COL_SELF_FUND].find({})
    l_result = list(result)
    l = []
    for item in l_result:
        l.append(item['code'])
    ds.daily_stk_list(l)
    for stk in l:
        try:
            df_train_total, data_train, df_test_total, data_test = ps.prepare_data_from_csv(test_stk=stk, train_stk=stk)
            # ps.prediction(df_test=df_test_total, orient_data_test=data_test,test_code=stk)
            pass
        except Exception as e:
            print(e)
            continue

    pass



def run2():
    df = sys_fund.sys_all_strategy_fund()

    for index, value in df.iterrows():
        try:
            code = value[EMVar.stock_code]
            df = ds.predict_data_source(stkcode=code, to_csv=False)['data']
            df_test_origin, test_data_source, test_total_size = ps.prepare_test_data(df)
            buyModel, _ = ps.prediction(df_test=df_test_origin, orient_data_test=test_data_source,
                                        test_code=code, to_csv=False, to_lean=False)
            ps.reuse_scope = True
            # buyModel.save_to_mongo(tag=value['tag'])
        except Exception as e:
            ps.reuse_scope = True
            print('预测失败')
            print(e)
            continue

def run3():
    from LSTM import predictor
    df = sys_fund.sys_all_strategy_fund()

    for index, value in df.iterrows():
        code = value[EMVar.stock_code]
        predictor.predict_indicator(code, ktype='D')



def train():
    from LSTM import predictor
    df = sys_fund.sys_all_strategy_fund()
    df = df[10:]
    i = 0
    for index, value in df.iterrows():
        if i <10:
            code = value[EMVar.stock_code]
            predictor.train_indicator(code,ktype='D')
            i += 1

# run()
# run2()
run3()

# train()